<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/17
 * Time: 14:54
 */

namespace Admin\Model;


class BookManageModel extends BaseModel
{
    // 数据表名（不包含表前缀）
    protected $tableName        =   'bookinfo';


    /**
     * 完成借阅将借阅数量减少1总数量加1
     * @param Integer $book_id 图书ID
     * @return boolean
    **/
    public function setBookBorrowNumber($book_id){
        $this->where("book_id=$book_id")->setInc('book_number');
        $this->where("book_id=$book_id")->setDec('book_borrow');
    }
    /**
     * 查询所有书籍信息
     * @return array()
    **/
    public function getAllBookInfo(){
        return $this->where("book_del=1")->order('book_id DESC')->select();
    }
    /**
     * 查询一条书籍信息
     * @return array()
    **/
    public function getBookSaveInfo($book_id){
        return $this->where("book_id=$book_id")->find();
    }

    /**
     * 查询书籍表所有信息
     * @return array()
    **/
    public function getBookShowInfo($book_name,$book_type_id,$book_case_id,$book_isbn_id){
        $where=array();
        if(!empty($book_name)){
            $where['book_name']=array('LIKE',"%{$book_name}%");
        }
        if(!empty($book_type_id)){
            $where['book_type_id']=$book_type_id;
        }
        if(!empty($book_case_id)){
            $where['book_case_id']=$book_case_id;
        }
        if(!empty($book_isbn_id)){
            $where['book_ISBN_id']=$book_isbn_id;
        }
        return $this->where($where)->order('book_id DESC')->select();
    }
    /**
     * 根据id查询书籍表的信息
    **/
    public function getBookByIdInfo($book_id){
        return $this->where("book_id = '$book_id'")->find();
    }
    /**
     * 书籍下架
     * @param Integer $book_id 书籍ID
     * @return boolean
    **/
    public function bookShowDel($book_id){
        $res=$this->where("book_id=$book_id")->setField('book_del',2);
        return $res == false ? false:true;
    }
    /**
     * 书籍上架
     * @param Integer $book_id 书籍ID
     * @return boolean
     **/
    public function bookShowRec($book_id){
        $res=$this->where("book_id=$book_id")->setField('book_del',1);
        return $res == false ? false:true;
    }
    /**
     * 书籍名称查询
     * @param String $book_name 书籍名称
     * @return boolean
    **/
    public function getBookName($book_id,$book_name){
        if(!empty($book_id)){
            $bookName=$this->where("book_id=$book_id")->getField('book_name');
            if($bookName==$book_name){
                return false;
            }
        }
        $res=$this->where("book_name LIKE '$book_name'")->select();
        if($res==false){
            return false;
        }else{
            return true;
        }
    }
    /**
     * 书籍信息添加
     * @param Integer $a_id 录入者ID
     * @param String $book_name 书籍名称
     * @param String $book_barcode 条形码
     * @param Integer $book_type_id 书籍类型ID
     * @param Integer $book_isbn_id 出版社ID
     * @param Integer $book_case_id 书架ID
     * @param String $book_author 书籍作者
     * @param String $book_translator 书籍翻译者名称
     * @param Integer $book_price 书籍价格
     * @param Integer $book_number 录入数量
     * @param String $book_intime 录入时间
     * @param Integer $book_del 是否删除
     * @return Boolean
    **/
    public function bookInfoAdd($a_id,$book_name,$book_barcode,$book_type_id,$book_isbn_id,$book_case_id,$book_author,$book_translator,$book_price,$book_number){
        $data['a_id']=$a_id;
        $data['book_name']=$book_name;
        $data['book_barcode']=$book_barcode;
        $data['book_type_id']=$book_type_id;
        $data['book_ISBN_id']=$book_isbn_id;
        $data['book_case_id']=$book_case_id;
        $data['book_author']=$book_author;
        $data['book_translator']=$book_translator;
        $data['book_price']=$book_price;
        $data['book_number']=$book_number;
        $data['book_intime']=time();
        $data['book_del']=1;
        $res=$this->add($data);
        if($res>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据图书ID查询书籍数量
     * @return Integer book_number
    **/
    public function getBookNumber($book_id){
        return $this->where("$book_id=book_id")->getField("book_number");
    }
    /**
     * 书籍修改
     * @param String $book_id 图书id
     * @param String $book_name 书籍名称
     * @param Integer $book_type_id 书籍类型ID
     * @param Integer $book_isbn_id 出版社ID
     * @param Integer $book_case_id 书架ID
     * @param String $book_author 书籍作者
     * @param String $book_translator 书籍翻译者名称
     * @param Integer $book_price 书籍价格
     * @param Integer $book_number 录入数量
     * @return Boolean
     **/
    public function bookShowSave($book_id,$book_name,$book_type_id,$book_isbn_id,$book_case_id,$book_author,$book_translator,$book_price,$book_number){
        $data['book_name']=$book_name;
        $data['book_type_id']=$book_type_id;
        $data['book_ISBN_id']=$book_isbn_id;
        $data['book_case_id']=$book_case_id;
        $data['book_author']=$book_author;
        $data['book_translator']=$book_translator;
        $data['book_price']=$book_price;
        $bookNumber=$this->getBookNumber($book_id);
        if($bookNumber!=$book_number){
            $data['book_number']=$book_number+$bookNumber;
            $data['book_intime']=time();
            $data['book_del']=1;
            $res=$this->where("$book_id=book_id")->save($data);
            if($res>0){
                return true;
            }else{
                return false;
            }
        }else{
            $data['book_intime']=time();
            $data['book_del']=1;
            $res=$this->where("$book_id=book_id")->save($data);
            if($res>0){
                return true;
            }else{
                return false;
            }
        }

    }
}